/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ems.dao.AwardDao;
import com.thinkgem.jeesite.modules.ems.entity.Award;
import com.thinkgem.jeesite.modules.ems.entity.Join;

/**
 * 奖品管理Service
 * @author zad
 * @version 2017-10-23
 */
@Service
@Transactional(readOnly = true)
public class AwardService extends CrudService<AwardDao, Award> {

	@Resource
    private AwardDao awardDao;
	
	public Award get(String id) {
		return super.get(id);
	}
	
	public List<Award> findList(Award award) {
		return super.findList(award);
	}
	
	public Page<Award> findPage(Page<Award> page, Award award) {
		return super.findPage(page, award);
	}
	
	@Transactional(readOnly = false)
	public void save(Award award) {
		super.save(award);
	}
	
	// 批量插入奖品信息
	@Transactional(readOnly = false)
	public int saveBatch(Award award)
	{
		Date createDate = new Date();
		award.setCreateDate(createDate);
		award.setUpdateDate(createDate);
		return awardDao.insertBatch(award);
	}
	
	// 批量插入奖品信息
		@Transactional(readOnly = false)
		public int saveList(List<Award> awards)
		{
			return awardDao.insertList(awards);
		}
	
	
	// 将多个选手名称及对应id转换为list，用于后台批量插入
	public Award parseJionList(Award award)
	{
		if(null == award || StringUtils.isBlank(award.getJoiner()))
		{
			return null;
		}
		List<Map<String,String>> joinerList = new ArrayList<Map<String,String>>();
		String joiner = award.getJoiner();
		String []joinerArr = joiner.split(",");
		for(String j:joinerArr)
		{
			//joinerList.add(j);
			String id=IdGen.uuid();
			Map<String,String> map = new HashMap<String, String>();
			map.put("id", id);
			map.put("joiner", j);
			
			joinerList.add(map);
		}
		award.setJoinerList(joinerList);
		return award;
	}
	
	@Transactional(readOnly = false)
	public void delete(Award award) {
		super.delete(award);
	}
	
}